package com.newhope.eshop.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.newhope.eshop.common.cache.redis.MyRedisTemplate;
import com.newhope.eshop.common.cache.redis.RedisConstants;
import com.newhope.eshop.service.api.UserService;
import com.newhope.eshop.service.api.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "api/user")
@Slf4j
public class UserController {
    @Reference(check = false, version = "2.0.0")
    public UserService userService;
    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @RequestMapping(value = "info", method = RequestMethod.GET)
    public User getUserInfo() {
        User user = new User();
        user.setId(1l);
        user = userService.findUser(user);
        log.info("by id user:" + user);
        List<User> userList = userService.findAllUser();
        log.info("userList:" + userList);
        return user;

    }

    @RequestMapping(value = "call/{id}", method = RequestMethod.GET)
    public User getUserInfoByCall(@PathVariable Long id) {
        User user1 = userService.findUser(id);
        user1.setId(id);
        log.info("userList:" + user1);
        return user1;

    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public PageInfo<User> getUserInfoByPage(@RequestParam(value = "pageNo") Integer pageNo, @RequestParam(value = "pageSize") Integer pageSize) {

        PageInfo<User> userList = userService.findByPage(pageNo, pageSize);
        log.info("userList:" + userList);
        return userList;

    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String addUserInfoByPage() {
        User user = new User();
        user.setName("wos");

        userService.addUser(user);
        return "success";

    }

    @RequestMapping(value = "redis", method = RequestMethod.GET)
    public String getUserFromRedis() {
        myRedisTemplate.set(RedisConstants.USER_NAME, "xg", "XiaoGang");
        return myRedisTemplate.get(RedisConstants.USER_NAME, "xg");

    }
}
